home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!news1!ind-007-237-41
- From: dlmiller@iquest.net (Doug Miller)
- Subject: Re: PLEASE Help! :(
- X-Nntp-Posting-Host: ind-007-237-41.iquest.net
- Message-ID: <Do9GMv.BKF@iquest.net>
- Sender: news@iquest.net (News Admin)
- Organization: IQuest Network Services
- X-Newsreader: News Xpress Version 1.0 Beta #2.1
- References: <4hsqk2$4d7@deadbird.db.erau.edu>
- Date: Thu, 14 Mar 1996 14:11:02 GMT
-
- kochank@news.db.erau.edu (Konrad Kochan) wrote:
- +Xref: news1 comp.lang.c:70770
- +Path: news1!news.sprintlink.net!news.aus.sig.net!news.cfi.org!zilker.net!halley!news.mpd.tandem.com!tandem!pacbell.com!decwrl!pa.dec.com!decuac.dec.com!haven.umd.edu!darwin.sura.net!news.db.erau.edu!kochank
- +From: kochank@news.db.erau.edu (Konrad Kochan)
- +Newsgroups: comp.lang.c
- +Subject: PLEASE Help! :(
- +Date: 9 Mar 1996 20:44:50 GMT
- +Organization: Embry-Riddle Aeronautical University
- +Lines: 107
- +Message-ID: <4hsqk2$4d7@deadbird.db.erau.edu>
- +NNTP-Posting-Host: 155.31.1.1
- +NNTP-Posting-User: kochank
- +X-Newsreader: TIN [version 1.2 PL2]
- +
- +Howdy.. I have a program to write, but I'm clueless... Now I could
- +give you a 100 excuses and blame the instructor (and I should cause
- +all of it would be true.. :)
- +
- +Anyways, I'm clueless.. can someone, a good samaritan (or someone just
- +bored..) help me out!
- +
- +Here is the program statement..
- +
- +Write a C program to sort, using any sorting method, a text file
- +named RECORDS.DAT, of unknown length, of records organized as follows:
- +
- +Field 1. Contains an integer.
- +Field 2. Contains a string of exactly 20 characters.
- +Field 3. Contains a float followed by a newline character.
- +
- +Example of file organization:
- +
- +234 Numa Chaikin 33000.0
- + 75 advertisements 55.55
- +901 Shepard 15255.5
- + 3 Brook 120050.0
- +888 non-descriptive 0.0
- +
- +Sorting has to be done lexicographically, that is, by the second field
- +using alphabetical order. This means that:
- +
- +1. Upper-case and lower-case letters should be treated identically.
- + For example, the right order for a part of the list above is:
- + 888 non-descriptive 0.0
- + 234 Numa Chaikin 33000.0
- +
- +2. The only punctuation sign used in strings is a dash '-', which
- + should be ignored in sorting (but not removed from words).
- +And here is what I got so far.. which DOESN'T work (right or otherwise, I
- +can't even get the program to scan the right fields from the file.. :(
- +
- +#include <stdio.h>
- +#include <stdlib.h>
- +#include <string.h>
- +
- +/* Global Variable Definition */
- +
- +int IntArr[512][5];
- +char WordsArr1[512][10];
- +char WordsArr2[512][10];
- +float FloatArr[512][5];
- +char inbuff[80];
- +int index=0;
- +
- +/* Function declaration */
- +
- +void Welcome(void);
- +void Goodbye(void);
- +
- +main()
- +{
- +/* Next function opens a file, if the file is missing, it prints
- + an error message */
- +
- +int i;
- + FILE * fin, * fout;
- + if (!(fin = fopen("RECORDS.DAT", "r")))
- + {
-
- don't EVER give your users a horseshit error message like this.
-
- + printf("Something is wrong: Probably file missing.\n");
-
- use perror () to find out and display the *exact* problem.
-
-
- + exit(1); /* Program exits here if file is missing. */
- + }
- + printf("\nInput File = RECORDS.DAT\n");
- +/* This function prints a Welcome message */
- +
- +/* Welcome(); */
- +
- +/* This function scans in the array */
- +
- + while (fgets(inbuff,80,fin) !=NULL)
- +{
-
- This sscanf statement contains most of your problems:
-
- +sscanf(inbuff,"%d %s %s %4.2f", &IntArr[index],&WordsArr1[index],&WordsArr2[index],&FloatArr[index]);
-
- 1) by including spaces in your format specifiers, you have instructed the program to *demand*
- at least one space between each field, and it *will* fail if there isn't.
- 2) you are also *demanding* two strings between the decimal and the float, in contradiction to
- the program specification of one string. sscanf will fail if only one string is present.
- 3) your format %4.2f is arbitrary, justified neither by the terms of the assignment nor by the
- sample data you provided. This will also produce undesirable program behavior if the input
- field is too long.
- 4) the program specification is for a string of exactly 20 characters in the second field. You are
- not looking for a string of exactly 20 characters.
-
-
- +index++;
- +}
- +
- +/* Opens the output file */
- +
- +if (!(fout = fopen("test", "w")))
- + {
- + printf("Something is wrong: File might be write protected.\n");
- + exit(1); /* Program exits here if file can't be opened. */
- + }
- +
- + for (i=0; i<index; i++)
- + fprintf(fout, "%d %s %s %4.2f\n", IntArr[i], WordsArr1[i],WordsArr2[i],
- + FloatArr[i]);
- +
- +/* Prints the sorted array to the screen */
-
- excuse me? where did it get sorted?
-
- +
- + for (i=0; i<index; i++)
- + printf("Original array: %d %s %s %4.2f \n", IntArr[i],WordsArr1[i],
- +WordsArr2[i],FloatArr[i]);
- +fclose(fin);
- +fclose(fout);
- +}
- +If you can either give me hints, rewrite the above, or write a totaly
- +new program I would appreciate it.. I know its a lot to ask for (to do some-
- +one elses homework pretty much..) but I'm really clueless..
- +
-
- You're right, it *is* a lot to ask for.
-
- hints, gladly. (see above)
-
- rewrite it? or write you a new program? forget it, pal. you've gotta be kidding.
- you have to do this yourself if you expect to learn anything.
-
-
-